package com.summer.core.web.action.admin;

import java.io.File;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.ParentPackage;

import com.summer.core.action.BaseAction;
import com.summer.core.entity.STable;
import com.summer.core.service.FieldService;
import com.summer.core.service.TableService;
import com.summer.core.web.service.MenuService;

@ParentPackage("admin")
public class TableAction extends BaseAction<STable, TableService> {

	private List importList;
	
	private MenuService menuService;
	
	private FieldService fieldService;
	
	
	public STable getSTable() {
		return getBean();
	}

	

	public void setSTable(STable sTable) {
		setBean(sTable);
	}
	

	@Resource
	public void setFieldService(FieldService fieldService) {
		this.fieldService = fieldService;
	}



	@Resource
	public void setService(TableService service) {
		super.setService(service);
	}


	@Resource
	public void setMeunService(MenuService menuService) {
		this.menuService = menuService;
	}


	public List getImportList() {
		return importList;
	}


	public void setImportList(List importList) {
		this.importList = importList;
	}

	//获得要导入的表名
	public String importTable()
	{
		importList=service.getImportTableNames();
		return result("import");
	}
	
	/**
	 * index页面
	 * @return
	 */
	public String index()
	{
		return result("index");
	}
	
	public String update()
	{
		super.update();
		return ajaxJsonSuccessMessage("保存成功！");
	}
	
	
	public String createCode()
	{
		bean.setCreateDate(new Date());
		bean.setAuthor("wwwlike");
		ServletContext context=ServletActionContext.getServletContext();
		service.createCode(context,getBean());
		return ajaxJsonSuccessMessage("代码创建成功！");
	}
	
	public String middle()
	{
		return result("middle");
	}
	
	public String tableImport() throws ClassNotFoundException, SecurityException, InstantiationException, IllegalAccessException
	{
		String classPath=getRequest().getParameter("classPath");
		
		STable stable=new STable();
		stable.setClassName(classPath);
		stable.setName(getLastName(classPath));
		String id=service.save(stable);
		String pid=getRequest().getParameter("pid");
		
		//System.out.println(kk.length);
		menuService.addTableMenu(id,stable.getName(),pid);	
		fieldService.addTableFields(stable,classPath);
		return SUCCESS;
	}
	
	

}
